package atom.core1.object;

import java.beans.PropertyDescriptor;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;

import atom.core0.util.StringUtil;

/**
 * <p>
 * Title:JavaBean的支持类
 * </p>
 * <p>
 * Description:只能使用它的静态方法
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * @author
 * @version 1.0
 */
public class PrintUtil
{
	/**
	 * Log4J Logger for this class
	 */
	private static final Logger	logger	= Logger.getLogger(PrintUtil.class);

	private PrintUtil() {
	};

	/**
	 * 返回JavaBean的打印字符串，该字符串的格式是"PropertyName = PropertyValue"，主要用于调试
	 * 
	 * @param bean
	 * @return
	 */
	public static String getPrintString(Object bean)
	{

		// 如果为null，则返回null字符串
		if (bean == null)
		{
			return "null";
		}

		PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(bean.getClass());

		String printString = "";
		for (int i = 0; i < pd.length; i++)
		{
			try
			{
				printString = printString + pd[i].getName() + StringUtil.getSpacebar(15 - pd[i].getName().length())
				        + " = \"" + PropertyUtils.getSimpleProperty(bean, pd[i].getName()) + "\"\n";
			}
			catch (Exception e)
			{
				logger.error("PropertyUtils.getSimpleProperty()异常！Property为" + pd[i].getName());
			}
		}
		return printString;
	}


}
